#Setup---------------
library(tidyr)
library(ggplot2)
library(xtable)
library(stargazer)
rm(list=ls())
df <- read.csv("df_replication.csv")

#Country Name Variable---------
df$State<-ifelse(df$UserLanguage=="AR-E","Egypt",NA)
df$State<-ifelse(df$UserLanguage=="AR-SA","Saudi Arabia",df$State)
df$State<-ifelse(df$UserLanguage=="CH-CH","China",df$State)
df$State<-ifelse(df$UserLanguage=="CS","Czech Republic",df$State)
df$State<-ifelse(df$UserLanguage=="CH-T","Taiwan",df$State)
df$State<-ifelse(df$UserLanguage=="DE","Germany",df$State)
df$State<-ifelse(df$UserLanguage=="E-A","Australia",df$State)
df$State<-ifelse(df$UserLanguage=="E-IN" | df$UserLanguage=="HI","India",df$State)
df$State<-ifelse(df$UserLanguage=="E-N","Nigeria",df$State)
df$State<-ifelse(df$UserLanguage=="E-SA","South Africa",df$State)
df$State<-ifelse(df$UserLanguage=="E-US","United States",df$State)
df$State<-ifelse(df$UserLanguage=="EN","United Kingdom",df$State)
df$State<-ifelse(df$UserLanguage=="ES-ES","Mexico",df$State)
df$State<-ifelse(df$UserLanguage=="F-B" | df$UserLanguage=="NL","Belgium",df$State)
df$State<-ifelse(df$UserLanguage=="FR","France",df$State)
df$State<-ifelse(df$UserLanguage=="ID","Indonesia",df$State)
df$State<-ifelse(df$UserLanguage=="IT","Italy",df$State)
df$State<-ifelse(df$UserLanguage=="JA","Japan",df$State)
df$State<-ifelse(df$UserLanguage=="KO","South Korea",df$State)
df$State<-ifelse(df$UserLanguage=="PL","Poland",df$State)
df$State<-ifelse(df$UserLanguage=="PT-BR","Brazil", df$State)
df$State<-ifelse(df$UserLanguage=="SK","Slovakia",df$State)
df$State<-ifelse(df$UserLanguage=="SV","Sweden",df$State)
df$State<-ifelse(df$UserLanguage=="TR","Turkey",df$State)

#Table 1 (Respondents Per State)--------------------------
state_breakdown <- table(df$State)
write.table(state_breakdown,file = "state_breakdown.txt",sep=",",quote = FALSE, row.names = F )

#Figure 1 (Entanglement)--------------
df$TPNW_Binary<-ifelse(df$TPNW>3,1,0) #supports signing tpnw
df$USArsenalDecrease_Binary<-ifelse(is.na(df$USPolicies_3),0,df$USPolicies_3) #supports decreasing us nuclear arnsenal
df$NTaboo_Binary<-ifelse(df$NTaboo==4,1,0) #strongly disagree that n use can be justified
df$TrustUS_Binary<-ifelse(df$TrustUS>2,1,0) #somewhat/strongly trust us to make responsible choices
df$Prolif_Binary<-ifelse(df$PolicyApprove_Nuclear>2,1,0) #somewhat/strongly support prolif
df$RC_NApprove_Binary<-ifelse(df$RC_NApprove==2,1,df$RC_NApprove) #approve of us n use to respond to c attack on ally
df$RN_NApprove_Binary<-ifelse(df$RN_NApprove==2,1,df$RN_NApprove) #approve of us n use to respond to n attack on ally

df$DisarmamentIndex<-df$NTaboo_Binary+df$USArsenalDecrease_Binary+df$TPNW_Binary
df$DeterrenceIndex<-df$RN_NApprove_Binary+df$RC_NApprove_Binary+df$Prolif_Binary

df$Contradictory_Binary<-ifelse(df$DeterrenceIndex>0 & df$DisarmamentIndex>0,1,0)
df$Contradictory_Scale<-ifelse(df$DeterrenceIndex==0 | df$DisarmamentIndex==0,1,NA)
df$Contradictory_Scale<-ifelse(df$DeterrenceIndex==1 & df$DisarmamentIndex==1,2,df$Contradictory_Scale)
df$Contradictory_Scale<-ifelse(df$DeterrenceIndex==2 & df$DisarmamentIndex==2,3,df$Contradictory_Scale)
df$Contradictory_Scale<-ifelse(df$DeterrenceIndex==2 & df$DisarmamentIndex==1,3,df$Contradictory_Scale)
df$Contradictory_Scale<-ifelse(df$DeterrenceIndex==1 & df$DisarmamentIndex==2,3,df$Contradictory_Scale)
df$Contradictory_Scale<-ifelse(df$DeterrenceIndex==3 & df$DisarmamentIndex==3,4,df$Contradictory_Scale)
df$Contradictory_Scale<-ifelse(df$DeterrenceIndex==1 & df$DisarmamentIndex==3,4,df$Contradictory_Scale)
df$Contradictory_Scale<-ifelse(df$DeterrenceIndex==3 & df$DisarmamentIndex==1,4,df$Contradictory_Scale)
df$Contradictory_Scale<-ifelse(df$DeterrenceIndex==2 & df$DisarmamentIndex==3,4,df$Contradictory_Scale)
df$Contradictory_Scale<-ifelse(df$DeterrenceIndex==3 & df$DisarmamentIndex==2,4,df$Contradictory_Scale)

df$Contradictory_Scale_1<-ifelse(df$Contradictory_Scale==1,1,0)
df$Contradictory_Scale_2<-ifelse(df$Contradictory_Scale==2,1,0)
df$Contradictory_Scale_3<-ifelse(df$Contradictory_Scale==3,1,0)
df$Contradictory_Scale_4<-ifelse(df$Contradictory_Scale==4,1,0)

rm(d)
d<-matrix(data=NA, nrow=24, ncol=4); d
for(i in 1:24){
  d[i,1] <- mean(df$Contradictory_Scale_1[df$State==unique(df$State)[i]])  
  d[i,2] <- mean(df$Contradictory_Scale_2[df$State==unique(df$State)[i]])  
  d[i,3] <- mean(df$Contradictory_Scale_3[df$State==unique(df$State)[i]])  
  d[i,4] <- mean(df$Contradictory_Scale_4[df$State==unique(df$State)[i]])  
}
colnames(d) <- c("None","Mild","Moderate","High")
d<-as.data.frame(d)
d$State <- unique(df$State)

d <- d %>% pivot_longer(cols=c("None","Mild","Moderate","High"),
                        names_to='Attitude',
                        values_to='Value')

d$Attitude <- factor(d$Attitude, levels=c("None","Mild","Moderate","High"))

d$Location<-NA
for(i in 1:96){
  d$Location[i] <- ifelse(d$Attitude[i]=='High', d$Value[i] - 0.02, d$Location[i])
  d$Location[i] <- ifelse(d$Attitude[i]=='Moderate',d$Value[i] + d$Value[d$State==d$State[i]][4] - 0.02 ,d$Location[i])
  d$Location[i] <- ifelse(d$Attitude[i]=='Mild', d$Value[i] + d$Value[d$State==d$State[i]][3] + d$Value[d$State==d$State[i]][4] - 0.02,
                          d$Location[i])
  d$Location[i] <- ifelse(d$Attitude[i]=='None', d$Value[i] + d$Value[d$State==d$State[i]][3] + d$Value[d$State==d$State[i]][2] 
                          + d$Value[d$State==d$State[i]][4] - 0.02, d$Location[i])
}
d$Location
d$Value<-round(d$Value*100,2)
d$Location<-round(d$Location*100,2)
print(d,n=96)

d<-subset(d,d$State != "China")

#Graph 

ggplot(d,) + 
  ggtitle("") +
  geom_col(aes(y=Value, x=State, fill=Attitude)) + 
  scale_fill_manual(name = "Level of Contradiction",
                    values = c("Mild"="cornflowerblue",
                               "Moderate"="salmon2",
                               "High" = "firebrick",
                               "None"="lightgrey"), 
                    labels = c('Mild'='Mild',
                               'None'='None',
                               'Moderate'='Moderate',
                               'High'='High')) + 
  geom_text(aes(label = round(Value,2), x=State,y=Location)) + 
  xlab("")+
  ylab("")+
  theme_classic()+
  theme(axis.text.x = element_text(angle = 45, hjust=1,size=14),
        axis.text.y = element_text(angle = 90, vjust=0.5, hjust=0,size=14),
        plot.title = element_text(size=16)) 

#Appendix C (Correlates of Entanglement)----------------
df$Female<-ifelse(df$Gender==1,1,0)
df$Ideology<-ifelse(df$PoliticalIdeology==99,NA,df$PoliticalIdeology)
df$Education<-ifelse(df$Edu==99,NA,df$Edu)
df$HHI<-ifelse(df$Income==99,NA,df$Income)

mod1<-lm(data=df, Contradictory_Scale ~ Age + Female + Ideology + Education + HHI)
stargazer(mod1)

#Figure 2 (TPNW)---------------
table(df$TPNW,df$State)

df$TPNW_Support<-ifelse(df$TPNW>3,1,0)
df$TPNW_Neutral<-ifelse(df$TPNW==3,1,0)
df$TPNW_Oppose<-ifelse(df$TPNW<3,1,0)

#Data
rm(d)
d<-matrix(data=NA, nrow=24, ncol=3); d
for(i in 1:24){
  d[i,1] <- mean(df$TPNW_Support[df$State==unique(df$State)[i]])  
  d[i,3] <- mean(df$TPNW_Neutral[df$State==unique(df$State)[i]])  
  d[i,2] <- mean(df$TPNW_Oppose[df$State==unique(df$State)[i]])  
}
colnames(d) <- c("Support","Neither Support Nor Oppose","Oppose")
d<-as.data.frame(d)
d$State <- unique(df$State)

d <- d %>% pivot_longer(cols=c('Support', 'Neither Support Nor Oppose','Oppose'),
                        names_to='Attitude',
                        values_to='Value')

d$Attitude <- factor(d$Attitude, levels=c('Support', 'Neither Support Nor Oppose','Oppose'))

d$Location<-NA
for(i in 1:72){
  d$Location[i] <- ifelse(d$Attitude[i]=='Oppose', d$Value[i] - 0.02, d$Location[i])
  d$Location[i] <- ifelse(d$Attitude[i]=='Neither Support Nor Oppose',d$Value[i] + d$Value[d$State==d$State[i]][3] - 0.02 ,d$Location[i])
  d$Location[i] <- ifelse(d$Attitude[i]=='Support', d$Value[i] + d$Value[d$State==d$State[i]][3] + d$Value[d$State==d$State[i]][2] - 0.02,
                          d$Location[i])
}
d$Location
d$Value<-round(d$Value*100,2)
d$Location<-round(d$Location*100,2)
print(d,n=72)


#Graph 

ggplot(d,) + 
  ggtitle("Do you support or oppose the United Nations Treaty on the Prohibition of Nuclear Weapons, which seeks to ban all nuclear weapons worldwide?") +
  geom_col(aes(y=Value, x=State, fill=Attitude)) + 
  scale_fill_manual(name = "",
                    values = c("Support"="cornflowerblue",
                               "Oppose"="salmon2",
                               "Neither Support Nor Oppose"="lightgrey"), 
                    labels = c('Support'='Support',
                               'Neither Support Nor Oppose'= 'Neither Support Nor Oppose',
                               'Oppose'='Oppose')) + 
  geom_text(aes(label = round(Value,2), x=State,y=Location)) + 
  xlab("")+
  ylab("")+
  theme_classic()+
  theme(axis.text.x = element_text(angle = 45, hjust=1,size=14),
        axis.text.y = element_text(angle = 90, vjust=0.5, hjust=0,size=14),
        plot.title = element_text(size=16))


df$Female<-ifelse(df$Gender==1,1,0)
df$Veteran<-ifelse(df$MilExperience==1,1,0)
df$Conservative<-ifelse(df$PoliticalIdeology<8,df$PoliticalIdeology,NA)
df$PolElite<-ifelse(df$PolicyElite==1,1,0)
df$Education<-ifelse(df$Edu<7,df$Edu,NA)
df$Inc<-ifelse(df$Income<10,df$Income,NA)


#Appendix B (Sample Summary Statistics)--------------------------
agetab<-aggregate(df[,"Age"], list(df$State), median)
femaletab<-aggregate(df[,"Female"], list(df$State), mean)
veterantab<-aggregate(df[,"Veteran"], list(df$State), mean)
polelitetab<-aggregate(df[,"PolElite"], list(df$State), mean)
educationtab<-aggregate(df[,"Education"], list(df$State), mean, na.rm=T)
inctab<-aggregate(df[,"Inc"], list(df$State), mean,na.rm=T)
summarytab<-cbind(as.numeric(agetab[,2]),
                  round(as.numeric(femaletab[,2]),4)*100,
                  round(as.numeric(veterantab[,2]),4)*100,
                  round(as.numeric(polelitetab[,2]),4)*100,
                  round(as.numeric(educationtab[,2]),2),
                  round(as.numeric(inctab[,2]),2))
summarytab<-cbind(agetab[,1],summarytab)
colnames(summarytab)<-c("State","Median Age","% Female","% Veteran",
                        "% Policy Experience","Mean Education Level","Mean Income Level")

print(xtable(summarytab),type="html",file="summarytable.htm")

#Figure 4 (Taboo)-----
table(df$NTaboo)

df$NTaboo_1<-ifelse(df$NTaboo==1,1,0)
df$NTaboo_2<-ifelse(df$NTaboo==2,1,0)
df$NTaboo_3<-ifelse(df$NTaboo==3,1,0)
df$NTaboo_4<-ifelse(df$NTaboo==4,1,0)

#Data
rm(d)
d<-matrix(data=NA, nrow=24, ncol=4); d
for(i in 1:24){
  d[i,1] <- mean(df$NTaboo_1[df$State==unique(df$State)[i]])  
  d[i,2] <- mean(df$NTaboo_2[df$State==unique(df$State)[i]])  
  d[i,3] <- mean(df$NTaboo_3[df$State==unique(df$State)[i]])  
  d[i,4] <- mean(df$NTaboo_4[df$State==unique(df$State)[i]])  
}
colnames(d) <- c("Strongly Agree","Somewhat Agree","Somewhat Disagree","Strongly Disagree")
d<-as.data.frame(d)
d$State <- unique(df$State)

d <- d %>% pivot_longer(cols=c("Strongly Agree","Somewhat Agree","Somewhat Disagree","Strongly Disagree"),
                        names_to='Attitude',
                        values_to='Value')

d$Attitude <- factor(d$Attitude, levels=c("Strongly Agree","Somewhat Agree","Somewhat Disagree","Strongly Disagree"))

d$Location<-NA
for(i in 1:96){
  d$Location[i] <- ifelse(d$Attitude[i]=='Strongly Disagree', d$Value[i] - 0.01, d$Location[i])
  d$Location[i] <- ifelse(d$Attitude[i]=='Somewhat Disagree',d$Value[d$State==d$State[i]][3] + d$Value[d$State==d$State[i]][4] - 0.01 ,d$Location[i])
  d$Location[i] <- ifelse(d$Attitude[i]=='Somewhat Agree', d$Value[d$State==d$State[i]][2] + d$Value[d$State==d$State[i]][4] + d$Value[d$State==d$State[i]][3] - 0.01,
                          d$Location[i])
  d$Location[i] <- ifelse(d$Attitude[i]=='Strongly Agree', d$Value[i] + d$Value[d$State==d$State[i]][2]+ d$Value[d$State==d$State[i]][3] + d$Value[d$State==d$State[i]][4] - 0.01,
                          d$Location[i])
}
d$Location
d$Value<-round(d$Value*100,2)
d$Location<-round(d$Location*100,2)
print(d,n=96)


#Graph 
ggplot(d,) + 
  ggtitle("Do you agree or disagree with the following statement? The use of nuclear weapons can be morally justified.") +
  geom_col(aes(y=Value, x=State, fill=Attitude)) + 
  scale_fill_manual(name = "",
                    values = c("Strongly Agree"="salmon",
                               "Somewhat Agree"="darksalmon",
                               "Somewhat Disagree"="cornflowerblue",
                               "Strongly Disagree"="steelblue"), 
                    labels = c("Strongly Agree"="Strongly Agree",
                               "Somewhat Agree"="Somewhat Agree",
                               "Somewhat Disagree"="Somewhat Disagree",
                               "Strongly Disagree"="Strongly Disagree")) + 
  geom_text(aes(label = round(Value,2), x=State,y=Location)) + 
  xlab("")+
  ylab("")+
  theme_classic()+
  theme(axis.text.x = element_text(angle = 45, hjust=1,size=14),
        axis.text.y = element_text(angle = 90, vjust=0.5, hjust=0,size=14),
        plot.title = element_text(size=16))


#Figure 3 (Proliferation)-----
table(df$PolicyApprove_Nuclear)
df$Prolif<-ifelse(df$PolicyApprove_Nuclear>2,1,0)
table(df$Prolif)

df$Prolif_1<-ifelse(df$PolicyApprove_Nuclear==1,1,0)
df$Prolif_2<-ifelse(df$PolicyApprove_Nuclear==2,1,0)
df$Prolif_3<-ifelse(df$PolicyApprove_Nuclear==3,1,0)
df$Prolif_4<-ifelse(df$PolicyApprove_Nuclear==4,1,0)

#Data
rm(d)
d<-matrix(data=NA, nrow=24, ncol=4); d
for(i in 1:24){
  d[i,1] <- mean(df$Prolif_1[df$State==unique(df$State)[i]])  
  d[i,2] <- mean(df$Prolif_2[df$State==unique(df$State)[i]])  
  d[i,3] <- mean(df$Prolif_3[df$State==unique(df$State)[i]])  
  d[i,4] <- mean(df$Prolif_4[df$State==unique(df$State)[i]])  
}
colnames(d) <- c("Strongly Disapprove","Somewhat Disapprove","Somewhat Approve","Strongly Approve")
d<-as.data.frame(d)
d$State <- unique(df$State)

d <- d %>% pivot_longer(cols=c("Strongly Approve","Somewhat Approve","Somewhat Disapprove","Strongly Disapprove"),
                        names_to='Attitude',
                        values_to='Value')

d$Attitude <- factor(d$Attitude, levels=c("Strongly Approve","Somewhat Approve","Somewhat Disapprove","Strongly Disapprove"))

d$Location<-NA
for(i in 1:96){
  d$Location[i] <- ifelse(d$Attitude[i]=='Strongly Disapprove', d$Value[i] - 0.01, d$Location[i])
  d$Location[i] <- ifelse(d$Attitude[i]=='Somewhat Disapprove',d$Value[d$State==d$State[i]][3] + d$Value[d$State==d$State[i]][4] - 0.01 ,d$Location[i])
  d$Location[i] <- ifelse(d$Attitude[i]=='Somewhat Approve', d$Value[d$State==d$State[i]][2] + d$Value[d$State==d$State[i]][4] + d$Value[d$State==d$State[i]][3] - 0.01,
                          d$Location[i])
  d$Location[i] <- ifelse(d$Attitude[i]=='Strongly Approve', d$Value[i] + d$Value[d$State==d$State[i]][2]+ d$Value[d$State==d$State[i]][3] + d$Value[d$State==d$State[i]][4] - 0.01,
                          d$Location[i])
}
d$Location
d$Value<-round(d$Value*100,2)
d$Location<-round(d$Location*100,2)
print(d,n=96)


#Graph 
d_NotChina<-subset(d,d$State!="China")
ggplot(d_NotChina,) + 
  ggtitle("Imagine that your government decided to build (more) nuclear weapons. How much would you approve or disapprove of this policy?") +
  geom_col(aes(y=Value, x=State, fill=Attitude)) + 
  scale_fill_manual(name = "",
                    values = c("Strongly Disapprove"="salmon",
                               "Somewhat Disapprove"="darksalmon",
                               "Somewhat Approve"="cornflowerblue",
                               "Strongly Approve"="steelblue"), 
                    labels = c("Strongly Disapprove"="Strongly Disapprove",
                               "Somewhat Disapprove"="Somewhat Disapprove",
                               "Somewhat Approve"="Somewhat Approve",
                               "Strongly Approve"="Strongly Approve")) + 
  geom_text(aes(label = round(Value,2), x=State,y=Location)) + 
  xlab("")+
  ylab("")+
  theme_classic()+
  theme(axis.text.x = element_text(angle = 45, hjust=1,size=14),
        axis.text.y = element_text(angle = 90, vjust=0.5, hjust=0,size=14),
        plot.title = element_text(size=16))

#Figure 5 (Nuclear Use)-------------
df$RC_NApprove<-ifelse(df$RC_NApprove==2,1,df$RC_NApprove)
df$RN_NApprove<-ifelse(df$RN_NApprove==2,1,df$RN_NApprove)

#Data
rm(d)
d<-matrix(data=NA, nrow=24, ncol=2); d
for(i in 1:24){
  d[i,1] <- mean(df$RC_NApprove[df$State==unique(df$State)[i]])  
  d[i,2] <- mean(df$RN_NApprove[df$State==unique(df$State)[i]])  
}
colnames(d) <- c("Nuclear","Conventional")
d<-as.data.frame(d)
d$State <- unique(df$State)

d <- d %>% pivot_longer(cols=c("Nuclear","Conventional"),
                        names_to='Attitude',
                        values_to='Value')

d$Attitude <- factor(d$Attitude, 
                     levels=c("Nuclear","Conventional"))
print(d,n=72)

d$Value=d$Value*100

#Graph 
d_NotChina<-subset(d,d$State!="China")
d_NotChina<-subset(d_NotChina,d_NotChina$State!="India")
d_NotChina<-subset(d_NotChina,d_NotChina$State!="Indonesia")
d_NotChina<-subset(d_NotChina,d_NotChina$State!="Japan")
ggplot(d_NotChina,) + 
  ggtitle("Would you approve or disapprove of the United States using nuclear weapons in response to a ______ attack on an ally?") +
  geom_col(aes(y=Value, x=State, fill=Attitude),stat="identity",position = "dodge") + 
  scale_fill_manual(name = "",
                    values = c("Conventional"="cornflowerblue",
                               "Nuclear"="firebrick"), 
                    labels = c('Nuclear'='Nuclear',
                               'Conventional'= 'Conventional')) + 
  geom_text(aes(label = round(Value,0), x=State,y=Value+3,group=Attitude),
            position = position_dodge(width=0.9),vjust=1) + 
  xlab("")+
  ylab("Percentage of respondents approving")+
  theme_classic()+
  theme(axis.text.x = element_text(angle = 45, hjust=1,size=14),
        axis.text.y = element_text(angle = 90, vjust=0.5, hjust=0,size=14),
        plot.title = element_text(size=16))


